<!DOCTYPE html>
<meta charset="utf-8">
<title>Legacy platform objects [[DefineOwnProperty]] method</title>
<link rel="help" href="https://heycam.github.io/webidl/#legacy-platform-object-defineownproperty">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./helper.js"></script>
<script>

test(function() {
  let span = document.createElement("span");
  span.className = "foo";
  // DOMTokenList supports an indexed property getter but not a setter.
  let domTokenList = span.classList;
  // Confirm the test settings.
  assert_equals(domTokenList.length, 1);
  assert_prop_desc_equals(domTokenList, "0",
                          {value: "foo", writable: false, enumerable: true,
                           configurable: true});
  assert_prop_desc_equals(domTokenList, "1", undefined);
  // Actual test
  assert_throws_js(TypeError, () =>
      Object.defineProperty(domTokenList, "0", {value: true, writable: true}));
  assert_throws_js(TypeError, () =>
      Object.defineProperty(domTokenList, "1", {value: true, writable: true}));
  assert_throws_js(TypeError, () =>
      Object.defineProperty(domTokenList, "0", {get: () => {}}));
  assert_throws_js(TypeError, () =>
      Object.defineProperty(domTokenList, "0", {set: () => {}}));
  assert_throws_js(TypeError, () =>
      Object.defineProperty(domTokenList, "1", {get: () => {}}));
  assert_throws_js(TypeError, () =>
      Object.defineProperty(domTokenList, "1", {set: () => {}}));
  assert_equals(domTokenList[0], "foo");
  assert_equals(domTokenList[1], undefined);
  domTokenList[0] = "bar";
  domTokenList[1] = "bar";
  assert_equals(domTokenList[0], "foo");
  assert_equals(domTokenList[1], undefined);
  assert_throws_js(TypeError, () => {
    "use strict";
    domTokenList[0] = "bar";
  });
  assert_throws_js(TypeError, () => {
    "use strict";
    domTokenList[1] = "bar";
  });
  // Nothing must change after all.
  assert_equals(domTokenList.length, 1);
  assert_prop_desc_equals(domTokenList, "0",
                          {value: "foo", writable: false, enumerable: true,
                           configurable: true});
  assert_prop_desc_equals(domTokenList, "1", undefined);
}, "Test [[DefineOwnProperty]] with no indexed property setter support.");

test(function() {
  // HTMLSelectElement supports an indexed property setter.
  let select = document.createElement("select");
  let option0 = document.createElement("option");
  let option1 = document.createElement("option");
  // Confirm the test settings.
  assert_equals(select.length, 0);
  assert_prop_desc_equals(select, "0", undefined);
  // Try to define an accessor property with non supported property index.
  assert_throws_js(TypeError, () =>
      Object.defineProperty(select, "0", {get: () => {}}));
  assert_throws_js(TypeError, () =>
      Object.defineProperty(select, "0", {set: () => {}}));
  assert_prop_desc_equals(select, "0", undefined);
  // writable, enumerable, configurable will be ignored.
  Object.defineProperty(select, "0", {value: option0, writable: false,
                                      enumerable: false, configurable: false});
  assert_prop_desc_equals(select, "0",
                          {value: option0, writable: true, enumerable: true,
                           configurable: true});
  select[1] = option1;
  assert_prop_desc_equals(select, "1",
                          {value: option1, writable: true, enumerable: true,
                           configurable: true});
  // Try to define an accessor property with a supported property index.
  assert_throws_js(TypeError, () =>
      Object.defineProperty(select, "0", {get: () => {}}));
  assert_throws_js(TypeError, () =>
      Object.defineProperty(select, "0", {set: () => {}}));
  assert_prop_desc_equals(select, "0",
                          {value: option0, writable: true, enumerable: true,
                           configurable: true});
}, "Test [[DefineOwnProperty]] with indexed property setter support.");
</script>
